package com.shouer.doc.ui.conversation.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.shouer.doc.R;
import com.shouer.doc.common.ShouerConst;
import com.shouer.doc.common.util.FileUtil;
import com.shouer.doc.common.util.ScreenUtils;
import com.shouer.doc.ui.photopreview.PhotoPreviewActivity;

import java.io.Serializable;
import java.util.List;

/**
 * Created by YYY on 2016/12/6.
 */

public class PatRecordImageAdapter extends RecyclerView.Adapter<PatRecordImageViewHolder> implements View.OnClickListener{

    private Context context;
    private List<String> imgUrls;
    private LayoutInflater mInflater;
    private int itemSize, imgSize;
    private ResizeOptions resizeOptions;

    public PatRecordImageAdapter(Context context, List<String> imgUrls) {
        this.context = context;
        this.imgUrls = imgUrls;
        mInflater=LayoutInflater.from(context);
        itemSize = (ScreenUtils.getScreenWidth(context) - ScreenUtils.dip2px(context, 48)) / 4;
        imgSize = itemSize * 2 / 3;
        resizeOptions = new ResizeOptions(imgSize, imgSize);
    }

    @Override
    public PatRecordImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=mInflater.inflate(R.layout.activity_conversation_img_layout,null);
        return new PatRecordImageViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(PatRecordImageViewHolder holder, int position) {

        String imgUrl=imgUrls.get(position);
        Uri imgUri=null;
        if (imgUrl.startsWith("http")||imgUrl.startsWith("https"))
        {
            imgUri=Uri.parse(imgUrl);
        }else
        {
            imgUri=Uri.parse(FileUtil.connectFilePath(ShouerConst.RES_SERVER,imgUrl));
        }
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(imgUri).setResizeOptions(resizeOptions).setAutoRotateEnabled(true).build();
        PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder().setOldController(holder.imgView.getController())
                .setImageRequest(request)
                .build();
        holder.imgView.setController(controller);
        holder.imgView.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        if (null==imgUrls)
        {
            return 0;
        }else
        {
            return imgUrls.size();
        }
    }

    public void setImgUrls(List<String> imgUrls) {
        this.imgUrls = imgUrls;
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View view) {
        Intent intent=new Intent(context, PhotoPreviewActivity.class);
        intent.putExtra("photos", (Serializable) imgUrls);
        context.startActivity(intent);
    }
}
